/*
 * Copyright (C) 2023 ab_skywalker@163.com All Rights Reserved.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
#include "foc_position.h"
#include "foc_pid.h"
#include "foc_model.h"
#include "foc_para.h"
#include "foc_filter.h"
#include "foc_velocity.h"

#include <string.h>

FOC_PID_S PidPos;
int32_t targetPos = 0;

void focPositionLoop(uint16_t period)
{
    int32_t targetVel = 0;
    int32_t curpos = 0;
    curpos = MotorWorkPara.AccumulatePhase;
    targetVel = pidCtrlBlock(targetPos, curpos, &PidPos);
    printf("%d,%d\r\n",targetPos-curpos,targetVel);
    focUpdateVel(targetVel);
}

void focPositionInit(void)
{
    memset(&PidPos, 0, sizeof(PidPos));
    PidPos.Kp = 4;
    PidPos.Ki = 0;
    PidPos.Kd = 0;
    PidPos.IntegralMax = 80;
    PidPos.IntegralMin = -80;
    PidPos.OutputMax = 80;
    PidPos.OutputMin = -80;
    PidPos.LastInput = 0;
    PidPos.Divide = 2048;
}

void focUpdatePos(int32_t pos)
{
    targetPos = pos;
}
